﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.BLL;
using Code.Models;
using QianBuo.Lottery.Web.Models;
using System.IO;
using System.Web.Security;

namespace QianBuo.Lottery.Web.Controllers
{
    public class CommenController : Controller
    {
        //
        // GET: /Commen/

        public ActionResult Index()
        {
            return View();
        }



        public ActionResult City(int? id,string colname)
        {
            CityManage CM = new CityManage();

            var citys = CM.Select(s => s.Select(p => new { CityID = p.ID, p.CityName, p.ProvinceID, p.Province.ProvinceName })).ToArray();
            var str = new System.Text.StringBuilder("var Provinces=[];\r\n");
            str.AppendLine("Provinces[0]={ID:'-省份-',Name:'-省份-',Citys:[{ID:'-城市-',Name:'-城市-'}]};");
            var tmp = citys.GroupBy(p => new { p.ProvinceID, p.ProvinceName });
            foreach (var item in tmp)
            {
                str.AppendFormat("Provinces[{0}]={{ID:{0},Name:'{1}',Citys:[{{ID:'-城市-',Name:'-城市-'}},{2}]}};\r\n", item.Key.ProvinceID, item.Key.ProvinceName, string.Join(",", item.Select(p => string.Format("{{ID:{0},Name:'{1}'}}", p.CityID, p.CityName)).ToArray()));
            }
            if (id.HasValue)
            {
                var city=citys.FirstOrDefault(p => p.CityID == id.Value);
                if (city != null)
                {
                    ViewData["cityid"] = city.CityID;
                    ViewData["provid"] = city.ProvinceID;
                }
            }

            if (!string.IsNullOrEmpty(colname))
                ViewData["ColName"]=colname;
            
            ViewData["Provinces"] = str.ToString();
            return View();
        }
        
        public ActionResult Bank(int? id, string controlName)
        {
            controlName = string.IsNullOrEmpty(controlName) ? "BankID" : controlName;
            CommenManage<Bank> cm = new CommenManage<Bank>();
            var bankMode = new BankModel
            {
                Banks = cm.Select(s => s.Select(p => new BankModel.Bank { ID = p.ID, Name = p.Name, Selected = id.HasValue&&id == p.ID })),
                Name = controlName
            };
            return View("Bank", bankMode);
        }


    }
}
